package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_ONE_KEY_FIGHT_SETTLE_FROM_CROSS, desc = "一键挑战(自动打完剩余进度)结算,跨服返回")
public class DouLuoDaLuOneKeyFightSettleCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuAutoBattleSettleRespMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuAutoBattleSettleRespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();

        if(reqMsg.hasConsume() && !StringUtils.isNullOrEmpty(reqMsg.getConsume())) {
            //元宝没有预扣
            //player.getModule(PreReduceModule.class).restorePreReduce(reqMsg.getConsume());
        }

        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();

        if(ret == 0){
            //扣除消耗
            if(reqMsg.hasConsume() && !StringUtils.isNullOrEmpty(reqMsg.getConsume())) {
                player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(reqMsg.getConsume()), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuBuyBuffConsume);
            }

            //连胜奖励
            if(reqMsg.hasPatronsExp()){
                PatronsModule patronsModule = player.getModule(PatronsModule.class);
                UserPatrons userPatrons = patronsModule.getUserPatrons(reqMsg.getPatronsId());
                if(userPatrons != null){
                    userPatrons.setBaiYeExp(reqMsg.getPatronsExp() + userPatrons.getBaiYeExp());
                    patronsModule.syncPatronsData(userPatrons);
                }
            }
            //连胜奖励
            if(reqMsg.hasWinRewards()) {
                player.getModule(CurrencyModule.class).addCurrency(reqMsg.getWinRewards(), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuBattleLianShengReward);
            }

            //结算奖励
            if(reqMsg.hasSettle()){
                DouLuoDaLuProto.DouLuoDaLuBattleSettlementTem settle = reqMsg.getSettle();
                player.getModule(CurrencyModule.class).addCurrency(reqMsg.getSettle().getRewards(), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuBattleSettleReward);

                if(reqMsg.hasLog()) {
                    DouLuoDaLuMgr.addPatronsBattleSettleLog(player, reqMsg.getLog(), settle, true);
                }
            }

            if(reqMsg.hasAllScore()){
                player.getModule(DouLuoDaLuModule.class).updateConditionValue(reqMsg.getAllScore(), eGamePlayerEventType.DouLuoDaLuPhaseTwoUserScoreCrossRank.getValue());
            }

            if(reqMsg.hasScore() && config != null){
                //player.getModule(CurrencyModule.class).addCurrency(config.SCORE_ITEM_ID_FIGHT + "=" + reqMsg.getScore(), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuBattleSettleReward);
            }
        }
        player.sendPacket(Protocol.U_DOU_LUO_DA_LU_ONE_KEY_FIGHT, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
